package codepulse.blogmoon.cn.common.utils;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;


import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
public class BaseConvert {
    public static <T,S> S convert(T source, Class<S> clazz) {

        if (null == source) {
            return null;
        }
        S target = null;
        try {
            target = clazz.newInstance();
            BeanUtils.copyProperties(source,target);
        } catch (Exception e) {
//            log.error("BaseConverter convert error, t = {}, class = {}", source, clazz, e);
        }
        return target;
    }

    public static <T,S> List<S> convertList(List<T> t, Class<S> clazz) {
        if (CollectionUtils.isEmpty(t)) {
            return null;
        }
        return t.stream().map(o -> convert(o, clazz)).collect(Collectors.toList());
    }

    public static <T,S> List<S> convertList(List<T> t, Class<S> clazz, String[] ignoreProperties) {
        if (CollectionUtils.isEmpty(t)) {
            return null;
        }
        return t.stream().map(o -> convert(o, clazz)).collect(Collectors.toList());
    }
    public static <T,S> List<S> convertList(List<T> t, Function<T,S> function) {
        if (CollectionUtils.isEmpty(t)) {
            return null;
        }
        return t.stream().map(function::apply).collect(Collectors.toList());
    }





}
